package com.xy.core.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class DateUtil
{
  private static final Log logger = LogFactory.getLog(DateUtil.class);

  public static Calendar setStartDay(Calendar paramCalendar)
  {
    paramCalendar.set(11, 0);
    paramCalendar.set(12, 0);
    paramCalendar.set(13, 0);
    return paramCalendar;
  }

  public static Calendar setEndDay(Calendar paramCalendar)
  {
    paramCalendar.set(11, 23);
    paramCalendar.set(12, 59);
    paramCalendar.set(13, 59);
    return paramCalendar;
  }

  public static void copyYearMonthDay(Calendar paramCalendar1, Calendar paramCalendar2)
  {
    paramCalendar1.set(1, paramCalendar2.get(1));
    paramCalendar1.set(2, paramCalendar2.get(2));
    paramCalendar1.set(5, paramCalendar2.get(5));
  }

  public static String formatEnDate(Date paramDate)
  {
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
    return localSimpleDateFormat.format(paramDate).replaceAll("上午", "AM").replaceAll("下午", "PM");
  }

  public static Date parseDate(String paramString)
  {
    Date localDate = null;
    try
    {
      localDate = DateUtils.parseDate(paramString, new String[] { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd" });
    }
    catch (Exception localException)
    {
      logger.error("Pase the Date(" + paramString + ") occur errors:" + localException.getMessage());
    }
    return localDate;
  }
  
  /**
   * 
   * @param 输入日期
   * @param 增加天数
   * @return 输入日期+增加天数
   * @throws ParseException
   */
  public static Date addDate(Date d,long day) throws ParseException { 

	  long time = d.getTime(); 
	  day = day*24*60*60*1000; 
	  time+=day; 
	  return new Date(time); 

	  } 
  
  public static Date addMonth(Date d,int month)throws ParseException {
	  Calendar  g = Calendar.getInstance();  
      g.setTime(d);  
      g.add(Calendar.MONTH,+month);             
      Date d2 = g.getTime(); 
	  return d2;
  }
  
  /**
   * 计算两个日期的月数差
   * @param d1
   * @param d2
   * @return
   * @throws ParseException
   */
  public static int getMonthSpace(Date d1, Date d2)
 throws ParseException {
	  SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
      Calendar c = Calendar.getInstance();
      c.setTime(d1);
      int year1 = c.get(Calendar.YEAR);
      int month1 = c.get(Calendar.MONTH);
       
      c.setTime(d2);
      int year2 = c.get(Calendar.YEAR);
      int month2 = c.get(Calendar.MONTH);
       
      int result;
      if(year1 == year2) {
          result = month1 - month2;
      } else {
          result = 12*(year1 - year2) + month1 - month2;
      }
      return Math.abs(result);
	}
  
  
  public static int getDaySpace(Date d1, Date d2)
		  throws ParseException {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(sdf.format(d1)));
			long time1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(sdf.format(d2)));
			long time2 = cal.getTimeInMillis();
			long between_days = (time2 - time1) / (1000 * 3600 * 24);
			return Math.abs(Integer.parseInt(String.valueOf(between_days)));  
		} catch (java.text.ParseException e) {
		  // TODO Auto-generated catch block
		  e.printStackTrace();
		  return 0;
	  }    
  }
  
	  public static void main(String[] args) {
//		  System.out.println(DateUtil.getMonthSpace(DateUtil.addMonth(new Date(), 4),new Date()));
//		  System.out.println(DateUtil.getMonthSpace(new Date(),DateUtil.addMonth(new Date(), 4)));
		  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		try {
			Date d1 = sdf.parse("2014-02-20 23:59:59");
			Date d2 = sdf.parse("2014-02-20 00:00:00");
			
//			Date d1 = new Date();
//			Thread.sleep(5000);
//			Date d2 = new Date();
			System.out.println(DateUtil.getDaySpace(d2, d1));
		} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
	}
}
 